﻿using System; 
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Function;
using Oracle.ManagedDataAccess.Client; 
using DbHelper;
using Helpers;

namespace ShenMaPrint
{
    public partial class Form5 : Form
    {
        [DllImport("user32.dll")]
        public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);//引用DLL
        
        Form1 f;
        public Form5(Form1 f1)
        {
            InitializeComponent();
            f = f1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确认打印？", "请确认！", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    if (textBox3.Text != "" && textBox2.Text != "")
                    {
                        MyOracleDataAccess dataAccessMain = null;
                        string conStr = INIFileHelper.ReadIniValue("DB", "CONNECTIONSTRING", Application.StartupPath + @"\Setting.ini").Trim();
                        dataAccessMain = new MyOracleDataAccess(conStr);

                        string code = textBox3.Text;
                        string datanow = "";
                        if (textBox4.Text == "" || textBox4.Text == null || textBox4.Text == "默认当日")
                        {
                            datanow = DateTime.Today.ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            datanow = textBox4.Text;
                        }
                        string Nowkind = "";
                        if (textBox1.Text == "" || textBox1.Text == null || textBox1.Text == "默认当前")
                        {
                            Nowkind = INIFileHelper.ReadIniValue("Setting", "Nowkind", Application.StartupPath + @"\Setting.ini").Trim();
                        }
                        else
                        {
                            Nowkind = textBox1.Text;
                        }
                        string zl = textBox2.Text;

                        f.PrintLabel(code, "重量：" + zl + "KG", datanow, "胶种：" + Nowkind);
                        LogHelper.SaveLog(DateTime.Now.ToString() + "触发称重，" + "胶种：" + Nowkind + ",胶块码：" + code + ",重量" + zl + "KG", LogHelper.LogType.RecDataState, "打印记录");
                        if (Nowkind == "QJ65HBGK")
                        {
                            Nowkind = "10125892";
                        }
                        else if (Nowkind == "QJ65HBG")
                        {
                            Nowkind = "10124946";
                        }
                        //PRFunction.PR_Insert_TaskInfoInit_AndType(code, Nowkind, zl);
                        dataAccessMain.ExecuteNonQuery_Text(

                                                @"Insert into MES_KPWIPRUNCARD_R
                                            (ORGID,KPSN,PARTNO,QTY)
                                            VALUES
                                            (:ORGID,:KPSN,:PARTNO,:QTY)",
                                                new OracleParameter[]
                                        {
                                            MyOracleDataAccess.CreateParameter("ORGID", OracleDbType.Int32, 3201),
                                            MyOracleDataAccess.CreateParameter("KPSN", OracleDbType.NVarchar2, code),
                                            MyOracleDataAccess.CreateParameter("PARTNO", OracleDbType.NVarchar2, Nowkind),
                                            MyOracleDataAccess.CreateParameter("QTY", OracleDbType.Double, zl)
                                        });
                        Form1.f5 = null;
                        f.GetInfo();
                        this.Close();
                    }
                    else
                    {
                        MessageBoxTimeoutA((IntPtr)0, "未输入重量或胶块编码，请重试！", "提示", 0, 0, 3000);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.SaveLog("Error:" + ex.ToString() + "\r\n", LogHelper.LogType.ErrorInfo, "ErrorF5");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1.f5 = null;
            this.Close();
        }
    }
}
